import { fileURLToPath, URL } from 'node:url'
import { ConfigEnv, defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import WindiCSS from 'vite-plugin-windicss'

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [vue(), WindiCSS()],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    css: {
      // CSS 预处理器
      preprocessorOptions: {
        //define global scss variable
        scss: {
          javascriptEnabled: true,
          additionalData: `@use "@/styles/variables.scss" as *;`
        }
      }
    },
    server: {
      host: '0.0.0.0',
      port: Number(env.VITE_APP_PORT),
      open: false,
      // 反向代理解决跨域
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: 'http://127.0.0.1:8002',
          changeOrigin: true
          // rewrite: (path) => {
          // http://localhost:8000/v1/users/me => http://127.0.0.1/61700/users/me
          //   path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
          // }
        }
      }
    }
  }
})
